1 Data table with GT package

With the gt package, anyone can make wonderful-looking tables using the R programming language. The gt philosophy: we can construct a wide variety of useful tables with a cohesive set of table parts. These include the table header, the stub, the column labels and spanner column labels, the table body, and the table footer.

Code
library(gt)
Code
library(gt)
library(gtExtras)

# Define the start and end dates for the data range
start_date <- "2010-06-07"
end_date <- "2010-06-14"

# Create a gt table based on preprocessed
# `sp500` table data
sp500 |>
  dplyr::filter(date >= start_date & date <= end_date) |>
  dplyr::select(-adj_close) |>
  gt() |>
  tab_header(
    # title
    title = "S&P 500",
    # subtitle
    subtitle = glue::glue("{start_date} to {end_date}")
  ) |>
  # format currency
  fmt_currency() |>
  # format date
  fmt_date(columns = date, date_style = "wd_m_day_year") |>
  # format suffixing
  fmt_number(columns = volume, suffixing = TRUE) %>% 
 
  # foot note with link
   tab_source_note(
    gt_hyperlink('Reference', 'https://gt.rstudio.com/')
  ) %>% 

  # make images url to images
  #gt_img_rows(columns = image, img_source = "web", height = 120) %>% 
  # align columns
  cols_align(
  align = c("center"),
  columns = everything()
)
S&P 500
2010-06-07 to 2010-06-14
date open high low close volume
Mon, Jun 14, 2010 $1,095.00 $1,105.91 $1,089.03 $1,089.63 4.43B
Fri, Jun 11, 2010 $1,082.65 $1,092.25 $1,077.12 $1,091.60 4.06B
Thu, Jun 10, 2010 $1,058.77 $1,087.85 $1,058.77 $1,086.84 5.14B
Wed, Jun 9, 2010 $1,062.75 $1,077.74 $1,052.25 $1,055.69 5.98B
Tue, Jun 8, 2010 $1,050.81 $1,063.15 $1,042.17 $1,062.00 6.19B
Mon, Jun 7, 2010 $1,065.84 $1,071.36 $1,049.86 $1,050.47 5.47B
Reference

1.1 Packages that use or extend gt

There are several R packages that either use gt to generate tabular outputs or extend gt in amazing ways. Here is a short list of some of these great packages:

1.2 reference:

https://gt.rstudio.com/

2 Data table with reactable

Code
library(reactable)
library(reactablefmtr)
mtcars %>% reactable()%>% 
  # Main title
  reactablefmtr::add_title(
    title = 'Main title'
  ) %>% 
    # sub title
  reactablefmtr::add_subtitle(
    subtitle = 'sub title',
    font_weight = 'normal'
  )

Main title

sub title

2.1 reference:

https://www.youtube.com/watch?v=E3ubwU5Uyqw

3 Data table with kable

Code
library(knitr)
mtcars %>% head() %>% kable()
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1

3.1 pages data

Code
library(rmarkdown)
mtcars %>% paged_table()

3.2 reference:

https://rstudio.github.io/distill/tables.html

Back to top